home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2001 April / april_2001.iso / intercd / root / ^Palm / Games / eCross / src / LevelsManager.java < prev    next >
Encoding:
Java Source  |  2000-07-27  |  2.5 KB  |  109 lines

  1. /*
  2.  * LevelsManager.java - A levels manager for eCross
  3.  * Copyright (C) 2000 Romain Guy
  4.  * guy.romain@bigfoot.com
  5.  * www.jext.org
  6.  *
  7.  * This program is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU General Public License
  9.  * as published by the Free Software Foundation; either version 2
  10.  * of the License, or any later version.
  11.  *
  12.  * This program is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  * GNU General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU General Public License
  18.  * along with this program; if not, write to the Free Software
  19.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  20.  */
  21.  
  22. import waba.io.*;
  23.  
  24. /**
  25.  * This class is used to build levels from a database.
  26.  * @author Romain Guy <guy.romain@bigfoot.com>
  27.  * @version 1.0
  28.  */
  29.  
  30. public class LevelsManager
  31. {
  32.   // the levels database
  33.   private String dataBase;
  34.   private byte count;
  35.  
  36.   public LevelsManager(String dataBase)
  37.   {
  38.     this.dataBase = dataBase;
  39.   }
  40.  
  41.   public byte getLevelsCount()
  42.   {
  43.     return count;
  44.   }
  45.  
  46.   /**
  47.    * Sets the current level.
  48.    * @param level The level number in the database
  49.    */
  50.  
  51.   public Level getLevel(int level)
  52.   {
  53.     Catalog levels = new Catalog(dataBase, Catalog.READ_ONLY);
  54.     if (!levels.isOpen())
  55.       return null;
  56.     count = (byte) levels.getRecordCount();
  57.  
  58.     byte[] b;
  59.     String name;
  60.  
  61.     if (!levels.setRecordPos(level))
  62.     {
  63.       levels.close();
  64.       return null;
  65.     }
  66.  
  67.     byte[] high = new byte[2];
  68.     if (levels.readBytes(high, 0, 2) != 2)
  69.     {
  70.       levels.close();
  71.       return null;
  72.     }
  73.  
  74.     b = new byte[Level.NAME_SPACE];
  75.     if (levels.readBytes(b, 0, b.length) != b.length)
  76.     {
  77.       levels.close();
  78.       return null;
  79.     }
  80.  
  81.     int count = 0;
  82.     char[] c = new char[Level.NAME_SPACE];
  83.  
  84.     for (int i = 0; i < c.length; i++)
  85.     {
  86.       byte _data = b[i];
  87.       if (_data != -1)
  88.       {
  89.         c[i] = (char) _data;
  90.         count++;
  91.       } else
  92.         break;
  93.     }
  94.     name = new String(c, 0, count);
  95.  
  96.     b = new byte[levels.getRecordSize() - Level.NAME_SPACE - 2];
  97.     if (levels.readBytes(b, 0, b.length) != b.length)
  98.     {
  99.       levels.close();
  100.       return null;
  101.     }
  102.  
  103.     levels.close();
  104.     return new Level(high, name, b);
  105.   }
  106. }
  107.  
  108. // End of LevelsManager.java
  109.